package com.yanqu.road.server.manager.activity;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.rank.cross.CrossUserRank;
import com.yanqu.road.entity.rank.stronghold.CrossActivityUnionRank;
import com.yanqu.road.logic.bussiness.rank.CrossGeneralActivityRankBusiness;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.logic.cross.CrossGeneralActivityUnionRankModel;
import com.yanqu.road.logic.cross.CrossGeneralActivityUserRankListModel;
import com.yanqu.road.logic.helper.ActivityHelper;
import com.yanqu.road.server.TempMgr;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class CrossGeneralActivityRankMgr extends TempMgr {

    private static Map<Integer, Map<Integer, CrossGeneralActivityUserRankListModel>> crossUserRankModelMap;

    private static Map<Integer, Map<Integer, CrossGeneralActivityUnionRankModel>> crossUnionRankModelMap;

    private static List<Integer> activityTypeList;

    public static Map<Integer, CrossGeneralActivityUserRankListModel> getCrossActivityRankListModel(int activityId) {
        Map<Integer, CrossGeneralActivityUserRankListModel> modelMap = crossUserRankModelMap.get(activityId);
        return new HashMap<>(modelMap);
    }

    public static HashMap<Integer, CrossGeneralActivityUnionRankModel> getCrossActivityUnionRankListModel(int activityId) {
        Map<Integer, CrossGeneralActivityUnionRankModel> modelMap = crossUnionRankModelMap.get(activityId);
        if (modelMap == null) {
            return new HashMap<>();
        }
        return new HashMap<>(modelMap);
    }

    private void initCrossRankListModelMap() {
        synchronized (crossUserRankModelMap) {
            List<Integer> activityIdList = getOpenActivityInfoList();
            Map<Integer, Map<Integer, LinkedList<CrossUserRank>>> crossUserRankListMap = CrossGeneralActivityRankBusiness.getCrossGeneralRankListMap(activityIdList);
            for (Map.Entry<Integer, Map<Integer, LinkedList<CrossUserRank>>> entry : crossUserRankListMap.entrySet()) {
                Integer activityId = entry.getKey();
                for (Map.Entry<Integer, LinkedList<CrossUserRank>> dataEntry : entry.getValue().entrySet()) {
                    int conditionType = dataEntry.getKey();
                    CrossGeneralActivityUserRankListModel rankListModel = new CrossGeneralActivityUserRankListModel(activityId, true);
                    rankListModel.setConditionType(conditionType);
                    int maxRank = ActivityHelper.getMaxRankByConditionType(conditionType, ActivityMgr.getActivityConditionInfoList(activityId));
                    if (maxRank <= 0) {
                        maxRank = GameConfig.RANK_DEFAULT_MAX_COUNT;
                    }
                    rankListModel.setRankNum(maxRank);
                    rankListModel.setRankList(dataEntry.getValue());
                    Map<Integer, CrossGeneralActivityUserRankListModel> conditionTypeMap = crossUserRankModelMap.get(activityId);
                    if (conditionTypeMap == null) {
                        conditionTypeMap = new ConcurrentHashMap<>();
                        crossUserRankModelMap.put(activityId, conditionTypeMap);
                    }
                    conditionTypeMap.put(conditionType, rankListModel);
                }
            }

        }
    }

    private void initCrossUnionRankListModelMap() {
        synchronized (crossUnionRankModelMap) {
            List<Integer> activityIdList = getOpenActivityInfoList();
            Map<Integer, Map<Integer, LinkedList<CrossActivityUnionRank>>> crossUnionRankListMap = CrossGeneralActivityRankBusiness.getCrossActivityUnionRankListMap(activityIdList);
            for (Map.Entry<Integer, Map<Integer, LinkedList<CrossActivityUnionRank>>> entry : crossUnionRankListMap.entrySet()) {
                Integer activityId = entry.getKey();
                ActivityInfo activityInfo = ActivityMgr.getActivityInfo(activityId);
                if (activityInfo == null) {
                    getLogger().error("activityInfo is null, activityId:{}", activityId);
                    continue;
                }
                Map<Integer, CrossGeneralActivityUnionRankModel> modelMap = crossUnionRankModelMap.get(activityId);
                if (modelMap == null) {
                    modelMap = new ConcurrentHashMap<>();
                    crossUnionRankModelMap.put(activityId, modelMap);
                }
                activityInfo = ActivityMgr.getActivityInfo(activityId);
                for (Map.Entry<Integer, LinkedList<CrossActivityUnionRank>> dataEntry : entry.getValue().entrySet()) {
                    int conditionType = dataEntry.getKey();
                    CrossGeneralActivityUnionRankModel rankListModel = new CrossGeneralActivityUnionRankModel(activityId, conditionType);
                    if (activityInfo != null) {
                        if ((activityInfo.getType() == eActivityType.RankUnionWar.getValue() && conditionType == eGamePlayerEventType.RankUnionWarUnionScoreRank.getValue())
                                || (activityInfo.getType() == eActivityType.PeakStrongHold.getValue() && conditionType == eGamePlayerEventType.PeakStrongHoldUnionIntegralRank.getValue())) {
                            Comparator comparator = ActivityHelper.getUnionRankSorter(activityInfo.getType(), conditionType, true);
                            if (comparator != null) {
                                rankListModel = new CrossGeneralActivityUnionRankModel(activityId, conditionType, comparator);
                            }
                        }
                    }
                    int maxRank = ActivityHelper.getMaxRankByConditionType(conditionType, ActivityMgr.getActivityConditionInfoList(activityId));
                    if (maxRank <= 0) {
                        maxRank = GameConfig.RANK_DEFAULT_MAX_COUNT;
                    }
                    rankListModel.setRankNum(maxRank);
                    rankListModel.setRankList(dataEntry.getValue());
                    modelMap.put(dataEntry.getKey(), rankListModel);
                }
            }
        }
    }

    private List<Integer> getOpenActivityInfoList() {
        Map<Integer, ActivityInfo> activityInfoMap = ActivityMgr.getActivityMap();
        List<Integer> activityIdList = new ArrayList<>();
        for (ActivityInfo activityInfo : activityInfoMap.values()) {
            if (activityTypeList.contains(activityInfo.getType())) {
                activityIdList.add(activityInfo.getActivityId());
            }
        }
        return activityIdList;
    }

    public static CrossGeneralActivityUserRankListModel getCrossActivityRankListModel(int activityId, int conditionType) {
        Map<Integer, CrossGeneralActivityUserRankListModel> modelMap = crossUserRankModelMap.get(activityId);
        if (modelMap == null) {
            synchronized (crossUserRankModelMap) {
                modelMap = crossUserRankModelMap.get(activityId);
                if (modelMap == null) {
                    modelMap = new ConcurrentHashMap<>();
                    crossUserRankModelMap.put(activityId, modelMap);
                }
            }
        }
        CrossGeneralActivityUserRankListModel crossActivityRankListModel = modelMap.get(conditionType);
        if (crossActivityRankListModel == null) {
            synchronized (crossUserRankModelMap) {
                crossActivityRankListModel = modelMap.get(conditionType);
                if (crossActivityRankListModel == null) {
                    ActivityInfo activityInfo = ActivityMgr.getActivityInfoMayLoadDB(activityId);
                    if (null != activityInfo) {
                        crossActivityRankListModel = new CrossGeneralActivityUserRankListModel(activityInfo.getActivityId(), true);
                        crossActivityRankListModel.setConditionType(conditionType);
                        int maxRank = ActivityHelper.getMaxRankByConditionType(conditionType, ActivityMgr.getActivityConditionInfoList(activityInfo.getActivityId()));
                        if (maxRank <= 0) {
                            maxRank = GameConfig.RANK_DEFAULT_MAX_COUNT;
                        }
                        crossActivityRankListModel.setRankNum(maxRank);
                        modelMap.put(conditionType, crossActivityRankListModel);
                    }
                }
            }
        }
        return crossActivityRankListModel;
    }

    public static CrossGeneralActivityUnionRankModel getCrossActivityUnionRankListModel(int activityId, int conditionType) {
        Map<Integer, CrossGeneralActivityUnionRankModel> rankListModelMap = crossUnionRankModelMap.get(activityId);
        if (rankListModelMap == null) {
            synchronized (crossUnionRankModelMap) {
                rankListModelMap = crossUnionRankModelMap.get(activityId);
                if (rankListModelMap == null) {
                    rankListModelMap = new ConcurrentHashMap<>(2);
                    crossUnionRankModelMap.put(activityId, rankListModelMap);
                }
            }
        }
        CrossGeneralActivityUnionRankModel model = rankListModelMap.get(conditionType);
        if (model == null) {
            ActivityInfo activityInfo = ActivityMgr.getActivityInfo(activityId);
            if (activityInfo == null) {
                getLogger().error("activityInfo is null, activityId:{}", activityId);
                return null;
            }
            synchronized (crossUnionRankModelMap) {
                model = rankListModelMap.get(conditionType);
                if (model == null) {
                    activityInfo = ActivityMgr.getActivityInfo(activityId);
                    if (null != activityInfo) {
                        //巅峰商会战要排行还要用赚速
                        if((activityInfo.getType() == eActivityType.RankUnionWar.getValue() && conditionType == eGamePlayerEventType.RankUnionWarUnionScoreRank.getValue())
                        || (activityInfo.getType() == eActivityType.PeakStrongHold.getValue() && conditionType == eGamePlayerEventType.PeakStrongHoldUnionIntegralRank.getValue())){
                            Comparator comparator = ActivityHelper.getUnionRankSorter(activityInfo.getType(), conditionType, true);
                            if(comparator == null){
                                model = new CrossGeneralActivityUnionRankModel(activityId,conditionType);
                            }else {
                                model = new CrossGeneralActivityUnionRankModel(activityId,conditionType, comparator);
                            }
                            int maxRank = ActivityHelper.getMaxRankByConditionType(conditionType, ActivityMgr.getActivityConditionInfoList(activityInfo.getActivityId()));
                            if (maxRank <= 0) {
                                maxRank = GameConfig.RANK_DEFAULT_MAX_COUNT;
                            }
                            model.setRankNum(maxRank);
                            rankListModelMap.put(conditionType, model);
                        }else{
                            model = new CrossGeneralActivityUnionRankModel(activityId, conditionType);
                            int maxRank = ActivityHelper.getMaxRankByConditionType(conditionType, ActivityMgr.getActivityConditionInfoList(activityInfo.getActivityId()));
                            if (maxRank <= 0) {
                                maxRank = GameConfig.RANK_DEFAULT_MAX_COUNT;
                            }
                            model.setRankNum(maxRank);
                            rankListModelMap.put(conditionType, model);
                        }
                    }
                }
            }
        }
        return model;
    }

    public static void changeStrongholdUserRank(int activityId, CrossUserRank crossUserRank) {
        int conditionType = crossUserRank.getBigRankType();
        CrossGeneralActivityUserRankListModel rankListModel = getCrossActivityRankListModel(activityId, crossUserRank.getBigRankType());
        if (null == rankListModel) {
            synchronized (crossUserRankModelMap) {
                if (!crossUserRankModelMap.containsKey(activityId)) {

                }
            }
            rankListModel = getCrossActivityRankListModel(activityId, conditionType);
        }
        if (null != rankListModel) {
            rankListModel.rankChange(crossUserRank);
        }
    }

    public static void changeStrongholdUnionRank(int activityId, int conditionType, CrossActivityUnionRank unionRank, boolean isUnionDel) {
        CrossGeneralActivityUnionRankModel rankListModel = getCrossActivityUnionRankListModel(activityId, conditionType);
        if (null != rankListModel) {
            if (isUnionDel) {
                rankListModel.removeRank(unionRank.getUnionUid());
            } else {
                rankListModel.rankChange(unionRank);
            }
        }
    }

    public static int getCrossActivityRank(int activityId, int conditionType, long serverId, long userId) {
        int rank = -1;
        CrossGeneralActivityUserRankListModel rankListModel = getCrossActivityRankListModel(activityId, conditionType);
        if (null != rankListModel) {
            rank = rankListModel.getMyRank(userId, serverId);
        }
        int maxRank = GameConfig.RANK_DEFAULT_MAX_COUNT;
        ActivityInfo activityInfo = ActivityMgr.getActivityInfoMayLoadDB(activityId);
        if (null != activityInfo) {
            maxRank = ActivityHelper.getMaxRankByConditionType(conditionType, ActivityMgr.getActivityConditionInfoList(activityInfo.getActivityId()));
        }
        if (rank > maxRank) {
            rank = -1;
        }
        return rank;
    }

    public static int getCrossActivityUnionRank(int activityId, int conditionType, long serverId, String unionUid) {
        int rank = -1;
        CrossGeneralActivityUnionRankModel rankListModel = getCrossActivityUnionRankListModel(activityId, conditionType);
        if (null != rankListModel) {
            rank = rankListModel.getMyRank(serverId, unionUid);
        }
        /*int maxRank = GameConfig.RANK_DEFAULT_MAX_COUNT;
        ActivityInfo activityInfo = CrossStrongholdActivityMgr.getOpenActivityInfo(activityId);
        if (null != activityInfo) {
            maxRank = Math.max(ActivityHelper.getMaxRankByConditionType(conditionType, CrossStrongholdActivityMgr.getActivityConditionInfoList(activityInfo.getActivityId())),10);

        }
        if (rank > maxRank) {
            rank = -1;
        }*/
        return rank;
    }

    public static String getCrossActivityUnionRankTop(int activityId, int conditionType){
        CrossGeneralActivityUnionRankModel rankListModel = getCrossActivityUnionRankListModel(activityId, conditionType);
        if (null != rankListModel) {
            List<CrossActivityUnionRank> list = rankListModel.getRankList();
            if(list.size() > 0){
                return list.get(0).getUnionUid();
            }
        }
        return "";
    }

    /**
     * 获取跨服商会活动的前n名商会,lowestRank代表取到的最低排名
     */
    public static List<String> getCrossActivityTopUnions(int activityId, int conditionType, int lowestRank) {
        CrossGeneralActivityUnionRankModel rankModel = getCrossActivityUnionRankListModel(activityId, conditionType);
        if (null != rankModel) {
            List<CrossActivityUnionRank> list = rankModel.getRankList();
            List<String> topUnions = new ArrayList<>();
            int rankNum = lowestRank <= list.size() ? lowestRank : list.size();
            for (int i = 0; i < rankNum; i++) {
                topUnions.add(list.get(i).getUnionUid());
            }
            return topUnions;
        }
        return null;
    }

    @Override
    public boolean reloadData() throws Exception {
        activityTypeList = new ArrayList<>();
        activityTypeList.add(eActivityType.DaFuWeng.getValue());
        activityTypeList.add((eActivityType.BowArtActivity.getValue()));
        activityTypeList.add(eActivityType.GhostActivity.getValue());
        activityTypeList.add(eActivityType.HangHaiActivity.getValue());
        activityTypeList.add(eActivityType.DragonBoatRaceActivity.getValue());
        activityTypeList.add(eActivityType.WinterLoveActivity.getValue());
        activityTypeList.add(eActivityType.XiaowuRecallActivity.getValue());
        activityTypeList.add(eActivityType.SeacraftActivity.getValue());
        activityTypeList.add(eActivityType.RankUnionWar.getValue());
        activityTypeList.add(eActivityType.Tomb.getValue());
        activityTypeList.add(eActivityType.Twins.getValue());
        activityTypeList.add(eActivityType.CricketSeasonActivity.getValue());
        activityTypeList.add(eActivityType.PeakStrongHold.getValue());
        activityTypeList.add(eActivityType.PeakSeacraftActivity.getValue());

        crossUserRankModelMap = new ConcurrentHashMap<>();
        initCrossRankListModelMap();
        crossUnionRankModelMap = new ConcurrentHashMap<>();
        initCrossUnionRankListModelMap();
        return true;
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean save() {
        for (Map<Integer, CrossGeneralActivityUserRankListModel> modelMap : crossUserRankModelMap.values()) {
            for (CrossGeneralActivityUserRankListModel model : modelMap.values()) {
                model.save();
            }
        }
        for (Map<Integer, CrossGeneralActivityUnionRankModel> modelMap : crossUnionRankModelMap.values()) {
            for (CrossGeneralActivityUnionRankModel model : modelMap.values()) {
                model.save();
            }
        }
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }
}
